package com.ehsunbehravesh.mypasswords.gui;

import com.ehsunbehravesh.mypasswords.Domain;
import com.ehsunbehravesh.mypasswords.Logger;
import com.ehsunbehravesh.mypasswords.Utils;
import com.ehsunbehravesh.mypasswords.resource.ResourceLoader;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class AboutDialog extends javax.swing.JDialog {  
  public AboutDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    myInitComponent();
    //Utils.setCenterOfParent((JFrame) parent, this);
  }

  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMain = new javax.swing.JPanel();
        lblHome = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        lblMyHome = new javax.swing.JLabel();
        lblMainIcon = new javax.swing.JLabel();
        lblSystemInfo = new javax.swing.JLabel();
        btnDonate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("About MyPasswords");
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        pnlMain.setBackground(new java.awt.Color(254, 254, 254));

        lblHome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblHome.setForeground(new java.awt.Color(4, 17, 250));
        lblHome.setText("http://www.mypasswords7.com");
        lblHome.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblHome.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblHomeMouseClicked(evt);
            }
        });

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblTitle.setText("MyPasswords");

        lblMyHome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblMyHome.setForeground(new java.awt.Color(4, 17, 250));
        lblMyHome.setText("Survey!");
        lblMyHome.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblMyHome.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblMyHomeMouseClicked(evt);
            }
        });

        lblMainIcon.setText("icon");

        lblSystemInfo.setFont(new java.awt.Font("Arial Narrow", 0, 11)); // NOI18N
        lblSystemInfo.setForeground(new java.awt.Color(102, 102, 102));
        lblSystemInfo.setText("System Information");

        btnDonate.setText("Donate");
        btnDonate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDonateActionPerformed(evt);
            }
        });
        btnDonate.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnDonateKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
        pnlMain.setLayout(pnlMainLayout);
        pnlMainLayout.setHorizontalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMainIcon, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDonate))
                .addGap(26, 26, 26)
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblTitle)
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMyHome)
                            .addComponent(lblHome)
                            .addComponent(lblSystemInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE))))
                .addContainerGap())
        );
        pnlMainLayout.setVerticalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(lblMainIcon, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDonate))
                    .addGroup(pnlMainLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(lblTitle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblHome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblMyHome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblSystemInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(11, 11, 11))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void lblHomeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblHomeMouseClicked
    Utils.openURL("http://www.mypasswords7.com");
  }//GEN-LAST:event_lblHomeMouseClicked

  private void lblMyHomeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblMyHomeMouseClicked
    Utils.openURL("http://www.mypasswords7.com/survey");
  }//GEN-LAST:event_lblMyHomeMouseClicked

  private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    formKeyPress(evt);
  }//GEN-LAST:event_formKeyPressed

  private void btnDonateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDonateActionPerformed
    btnDonateActionPerform(evt);
  }//GEN-LAST:event_btnDonateActionPerformed

  private void btnDonateKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnDonateKeyPressed
    setVisible(false);
    dispose();
    System.gc();
  }//GEN-LAST:event_btnDonateKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDonate;
    private javax.swing.JLabel lblHome;
    private javax.swing.JLabel lblMainIcon;
    private javax.swing.JLabel lblMyHome;
    private javax.swing.JLabel lblSystemInfo;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JPanel pnlMain;
    // End of variables declaration//GEN-END:variables

  private void myInitComponent() {
    URL url = ResourceLoader.class.getResource("safe.png");
    ImageIcon safeIcon = new ImageIcon(url);
    setIconImage(safeIcon.getImage());
    
    Domain domain = Domain.getInstance();
    lblTitle.setText(lblTitle.getText() + " " + domain.getMyPasswordsVersion());

    url = ResourceLoader.class.getResource("safe128.png");
    lblMainIcon.setIcon(new ImageIcon(url));

    displaySystemInfo();
    
    /*
    MainFrame mainFrame = (MainFrame) getParent();
    if (mainFrame.getCurrentLanguage() != null) {
      changeLanguage(mainFrame.getCurrentLanguage());
    } */  
  }

  private void displaySystemInfo() {
    try {
      Properties systemProperties = System.getProperties();
      if (systemProperties != null) {
        lblSystemInfo.setText(String.format("<html><b>System Information</b><br/>OS: %s version %s %s<br/>Java Vendor: %s<br/>Java Version: %s</html>",
                systemProperties.get("os.name"),
                systemProperties.get("os.version"),
                systemProperties.get("os.arch"),
                systemProperties.get("java.vm.vendor"),
                systemProperties.get("java.specification.version")));
      }
    } catch (Exception ex) {
      Logger.log(ex.getMessage());
    }
  }

  private void formKeyPress(KeyEvent evt) {
    int key = evt.getKeyCode();
    if (key == 27) {
      closeDialog();
    }
  }

  private void closeDialog() {
    setVisible(false);
  }    
  
  private void changeLanguage(Properties currentLanguage) {
    try {
      setDirection(currentLanguage.getProperty("direction"));
      setTitle(currentLanguage.getProperty("AboutMyPassword"));      
      
      refresh();
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(this, "Error occured in changing language!", "Error", JOptionPane.ERROR_MESSAGE);
    }
  }
  
  private void setDirection(String direction) {
    if (direction.equals("rtl")) {
      setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      pnlMain.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      
      for (int i = 0; i < pnlMain.getComponents().length; i++) {
        Component component = pnlMain.getComponents()[i];
        component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      }
    } else if (direction.equals("ltr")) {
      setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
      pnlMain.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
      
      for (int i = 0; i < pnlMain.getComponents().length; i++) {
        Component component = pnlMain.getComponents()[i];
        component.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
      }
    }
  }

  private void refresh() {
    pnlMain.setVisible(false);
    pnlMain.setVisible(true);
  }

  private void btnDonateActionPerform(ActionEvent evt) {
    Utils.openURL("http://www.mypasswords7.com/donate");
  }
}
